/*
 * @lc app=leetcode id=1 lang=javascript
 *
 * [1] Two Sum
 *
 * https://leetcode.com/problems/two-sum/description/
 *
 * algorithms
 * Easy (42.82%)
 * Total Accepted:    1.6M
 * Total Submissions: 3.8M
 * Testcase Example:  '[2,7,11,15]\n9'
 *
 * Given an array of integers, return indices of the two numbers such that they
 * add up to a specific target.
 * 
 * You may assume that each input would have exactly one solution, and you may
 * not use the same element twice.
 * 
 * Example:
 * 
 * 
 * Given nums = [2, 7, 11, 15], target = 9,
 * 
 * Because nums[0] + nums[1] = 2 + 7 = 9,
 * return [0, 1].
 * 
 * 
 * 
 * 
 */
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
let nums = [2, 7, 11, 15], target = 9
var twoSum = function (nums, target) {
    // 第一种解法
    // for(let i = 0; i < nums.length; i++) {
    //     for(let j = i + 1; j < nums.length; j++) {
    //         if(nums[j] + nums[i] === target) {
    //             return [i, j]
    //         }
    //     }
    // }  

    // 第二种解法
    // let map = {};
    // for (let i = 0; i < nums.length; i++) {
    //     map[nums[i]] = i + '';
    // }

    // for (let i = 0; i < nums.length; i++) {
    //     var c = map[target - nums[i]];
    //     if (c && (c !== i + '')) return [i, c]
    // }

    // 第三种解法
    // var m = {}
    // for(var i = 0; i < nums.length; i++) m[nums[i]] = i
    // // console.log(m)
    // for(var i = 0; i < nums.length; i++) {
    //     var count = target - nums[i]
    //     if(m[count] && m[count] !== i) return [i, m[count]]
    // }

    // 第四种解法
    for (let i = 0; i < nums.length; i ++ ) {
        let index = nums.lastIndexOf(target - nums[i])
        if (index !== -1 && index !== i) {
            return [i, index];
        }
    }
};
console.log(twoSum(nums, target));


